package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteStrategyEsService;
import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.service.IStrategyContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.query.StrategyQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 攻略 Controller
 * 
 * @author dh
 * @date 2025-09-16
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyContentService contentService;
    @Autowired
    private RemoteStrategyEsService esService;

    @GetMapping("/dataInit")
    public R<?> dataInit(){
        for (Strategy strategy : strategyService.list()) {
            StrategyEs es = new StrategyEs();
            BeanUtils.copyProperties(strategy,es);
            esService.initStrategyEs(es,SecurityConstants.INNER);
        }
        return R.ok();
    }


    @RequiresLogin
    @PostMapping("/thumbsup")
    public R<Map<String,Object>> thumbsup(Long sid){
        return R.ok(strategyService.thumbsup(sid));
    }

    @GetMapping("/isUserFavor")
    public R<Boolean> isUserFavor(Long uid,Long sid){
        return R.ok(strategyService.isUserFavor(uid,sid));
    }
    @PostMapping("/favor")
    @RequiresLogin
    public R<Map<String,Object>> favor(Long sid){
        Map<String,Object> map = strategyService.favor(sid);
        return  R.ok(map);
    }

    @PostMapping("/replynumIncr")
    public R<Map<String,Object>> replynumIncr(Long sid){
        Map<String,Object> map =  strategyService.replynumIncr(sid);
        return R.ok(map);
    }

    @PostMapping("/viewnumIncr")
    public R<Map<String,Object>> viewnumIncr(Long sid){
        Map<String,Object> map =  strategyService.viewnumIncr(sid);
        return R.ok(map);
    }

    @GetMapping("/viewnnumTop3")
    public R<List<Strategy>> viewTop3(Long destId){
        List<Strategy> list = strategyService.queryViewTop3(destId);
        return R.ok(list);
    }

    /**
     * 攻略分类
     * @param destId 目的地id
     * @return 分类列表
     */
    @GetMapping("/catalogList")
    public R<List<StrategyCatalog>> getCatalogList(Long destId){
        List<StrategyCatalog> list = catalogService.queryCatalogList(destId);
        return R.ok(list);
    }
    /**
     * 攻略详情
     */
    @GetMapping("/detail/{id}")
    public R<Strategy> detail(@PathVariable Long id) {
        Strategy strategy = strategyService.getById(id);
        StrategyContent content = contentService.getById(id);
        strategy.setContent(content);
        return R.ok(strategy);
    }
    /**
     * 攻略 列表
     */
    @GetMapping("/query")
    public R<IPage<Strategy>> query(String orderBy,StrategyQuery qo,Long refid,Long type) {
        IPage<Strategy> page = strategyService.queryPage(orderBy,qo,refid,type);
        return R.ok(page);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Strategy>> feignList() {
        return R.ok(strategyService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Strategy> feignGet(@PathVariable Long id) {
        return R.ok(strategyService.getById(id));
    }

    @InnerAuth
    @PostMapping("/feign/statisRank")
    public R<Strategy> statisRank(){
//        System.out.println("StrategyController的statisRank的远程调用" );
        strategyService.statisRank();
        return R.ok();
    }

    @InnerAuth
    @PostMapping("/feign/statisCondition")
    public R<StrategyCondition>  statisCondition(){
        strategyService.statisCondition();
        return R.ok();
    }

    @PostMapping("/feign/strategyStatisHashPersistence")
    public R<StrategyCondition>  strategyStatisHashPersistence(){
        strategyService.strategyStatisHashPersistence();
        return R.ok();
    };
}
